package subject_set.offer100;

import java.util.PriorityQueue;

/**
 * @author haomin
 * @date 2022/10/20 15:50
 **/
public class Offer40 {
    class Solution {
        public int[] getLeastNumbers(int[] arr, int k) {
            if(arr.length == 0 || k == 0) return new int[0];
            PriorityQueue<Integer> q = new PriorityQueue<>((o1, o2) -> (o2-o1));
            for(int i : arr){
                if(q.size() < k) q.offer(i);
                else if(!q.isEmpty() && q.peek() > i){
                    q.poll();q.offer(i);
                }
            }
            return q.stream().mapToInt(Integer::intValue).toArray();
        }
    }
}